package state.component;

import state.GameConstants;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Manager;
import state.Sprite;
import state.TouchScreenListener;
import state.util.ImageUtil;
import iplayparticle.view.iplayparticleResources;

public class TouchPad extends Sprite implements TouchScreenListener {
  private GameImage[] touchImages = null;
  private GameImage[] touchBoldImages = null;

  private Sprite[] touchSprites = null;
  private int[] keyCodes = null;

  public TouchPad(Manager manager) {
    super(null, manager, 0, 0, GameConstants.TOP_LEFT_ANCHOR);
    this._manager = manager;
    touchImages = new GameImage[5];
    touchImages[0] = new GameImage(iplayparticleResources.image_Arrow_touch, null, null).setStatic(true);
    touchImages[1] = ImageUtil.rotate(touchImages[0], 90).setStatic(true);
    touchImages[2] = ImageUtil.rotate(touchImages[0], 180).setStatic(true);
    touchImages[3] = ImageUtil.rotate(touchImages[0], 270).setStatic(true);
    touchImages[4] = new GameImage(iplayparticleResources.image_fireButton_touch, null, null).setStatic(true);

    touchBoldImages = new GameImage[5];
    touchBoldImages[0] = new GameImage(iplayparticleResources.image_Arrow_touch_Bold, null, null).setStatic(true);
    touchBoldImages[1] = ImageUtil.rotate(touchBoldImages[0], 90).setStatic(true);
    touchBoldImages[2] = ImageUtil.rotate(touchBoldImages[0], 180).setStatic(true);
    touchBoldImages[3] = ImageUtil.rotate(touchBoldImages[0], 270).setStatic(true);
    touchBoldImages[4] = new GameImage(iplayparticleResources.image_fireButton_touch_bold, null, null).setStatic(true);

    touchSprites = new Sprite[5];
    touchSprites[0] = new Sprite(touchImages[0], null, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2 - 30, GameConstants.BOTTOM_HCENTER_ANCHOR);
    touchSprites[1] = new Sprite(touchImages[1], null, GameConstants.SCREEN_WIDTH / 2 + 30, GameConstants.SCREEN_HEIGHT / 2, GameConstants.VCENTER_LEFT_ANCHOR);
    touchSprites[2] = new Sprite(touchImages[2], null, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2 + 30, GameConstants.TOP_HCENTER_ANCHOR);
    touchSprites[3] = new Sprite(touchImages[3], null, GameConstants.SCREEN_WIDTH / 2 - 30, GameConstants.SCREEN_HEIGHT / 2, GameConstants.VCENTER_RIGHT_ANCHOR);
    touchSprites[4] = new Sprite(touchImages[4], null, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);

    for (int i = 0; i < 5; i++) {
      touchSprites[i].setId((byte) i);
    }

    keyCodes = new int[]{Key.UP, Key.RIGHT, Key.DOWN, Key.LEFT, Key.FIRE};
    setTouchScreenListener(this);
  }

  public Sprite paint(Graphics g) {
    g.setClip(0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
    for (int i = 0; i < 5; i++) {
      touchSprites[i].paint(g);
    }
    return this;
  }

  public void pointerPressed(Sprite source, int x1, int y1) {
    for (int i = 0; i < 5; i++) {
      if (touchSprites[i].contains(x1, y1)) {
        GameGlobal.systemCanvas.getKeyRepeatHandle().keyPressed(keyCodes[i]);
        touchSprites[i].setImage(touchBoldImages[i]);
        _manager.stopEvent();
        _manager.getParent().doKeyPressed(keyCodes[i]);
        break;
      }
    }
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    for (int i = 0; i < 5; i++) {
      if (touchSprites[i].contains(x1, y1)) {
        GameGlobal.systemCanvas.getKeyRepeatHandle().keyReleased();
        touchSprites[i].setImage(touchImages[i]);
        _manager.stopEvent();
        Key.isCommandKey = true;
        _manager.getParent().doKeyReleased(keyCodes[i]);
        break;
      }
    }
  }

  public void pointerDragged(Sprite source, int x1, int y1) {
  }

  public Sprite detroy() {
    for (int i = 0; i < 5; i++) {
      touchSprites[i].detroy();
    }
    return super.detroy();
  }
}
